#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //为按钮设置一个图标
//    QIcon icon(":/lh.png");
//    ui->pushButton->setIcon(icon);
//    ui->pushButton->setIconSize(QSize(50,50));
    //通过四个按钮控制一个按钮的移动
    ui->pushButton_target->setIcon(QIcon(":/img/dog.png"));
    ui->pushButton_target->setIconSize(QSize(100,100));

    ui->pushButton_up->setIcon((QIcon(":/img/up.png")));
    ui->pushButton_up->setIconSize(QSize(50,50));

    ui->pushButton_down->setIcon((QIcon(":/img/down.png")));
    ui->pushButton_down->setIconSize(QSize(50,50));

    ui->pushButton_right->setIcon((QIcon(":/img/right.png")));
    ui->pushButton_right->setIconSize(QSize(50,50));

    ui->pushButton_left->setIcon((QIcon(":/img/left.png")));
    ui->pushButton_left->setIconSize(QSize(50,50));

    //为按钮设置快捷键
    ui->pushButton_up->setShortcut(Qt::Key_W);
    ui->pushButton_up->setShortcut(QKeySequence(Qt::Key_W));
    ui->pushButton_down->setShortcut(Qt::Key_S);
    ui->pushButton_right->setShortcut(Qt::Key_D);
    ui->pushButton_left->setShortcut(Qt::Key_A);

    //设置长按按钮时的长按效果
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_up_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(),rect.y()-5,rect.width(),rect.height());
}

void Widget::on_pushButton_down_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x(),rect.y()+5,rect.width(),rect.height());
}

void Widget::on_pushButton_left_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x()-5,rect.y(),rect.width(),rect.height());
}

void Widget::on_pushButton_right_clicked()
{
    QRect rect = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(rect.x()+5,rect.y(),rect.width(),rect.height());
}
